$(document).ready(function() {


$('.mark-as-investment').hide();
$('.firstcheckbox').click( function(){
	if ( $(this).prop('checked') ) {
		$('.mark-as-investment').show();
	} else {
		$('.mark-as-investment').hide();
	}
});

//$('.firstcheckbox').trigger('click');
 
 
 $( "body" ).delegate( ".item-class", "click", function() {
 
 //$('.item-class').click( function(){
    if ($(this).hasClass('item-open') ) {
      var id = $(this).attr('href');
      $(id).hide('slow');
      $(this).removeClass('item-open');
      $(this).addClass('collapsed');
      return false;
    }
    $('.item-class').addClass('collapsed');

    $('.item-class').removeClass('item-open');
    $(this).addClass('item-open');

    $('.item-container').hide('slow');
    var id = $(this).attr('href');

    $(this).removeClass('collapsed');

    $(id).show('slow');
    return false;
 });
 
 
    Dropzone.autoDiscover = false;
    
    $('.propertytype').hide();
    
    //Drag n Drop up-loader for EXTRA_IMAGE
    var paramsdata = {};
    paramsdata['UNIQUEIDENTIFIER'] = UNIQUEIDENTIFIER;
   // paramsdata['property_id'] = $("#property_id").val();
   
    $(".extraimage_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'EXTRA_IMAGE',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );

    //Drag n Drop up-loader for THREE_SIXTY_IMAGE
    $(".three60image_dropzone").dropzone(
            {
               
                url: BASE_PATH + '/property/upload',
                paramName: 'THREE_SIXTY_IMAGE',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );



//Drag n Drop up-loader for EXTRA_IMAGE
    $(".floor_image_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'FLOOR_IMAGE',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );



//Drag n Drop up-loader for BEDROOM_IMAGE
    $(".bedroom_image_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'BEDROOM_IMAGE',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );


//Drag n Drop up-loader for LIVING_ROOM
    $(".living_image_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'LIVING_ROOM',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );



//Drag n Drop up-loader for BATHROOM
    $(".bathroom_image_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'BATHROOM',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );



//Drag n Drop up-loader for OUTDOOR
    $(".outdoor_image_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'OUTDOOR',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".jpeg,.jpg,.png,.gif,.JPEG,.JPG,.PNG,.GIF",
            }
    );



//Drag n Drop up-loader for DOCUMENT
    $(".document_dropzone").dropzone(
            {
                url: BASE_PATH + '/property/upload',
                paramName: 'DOCUMENT',
                params: paramsdata,
                maxFilesize: 500,
                acceptedFiles: ".ppt,.pptx,.xls,.xlsx,.odp,.doc,.docx,.pdf,.ppsx",
            }
    );


    //Select 2 initialization
    $("#member_id").select2();
    $("#prp_area").select2();
    
    $("#prp_secondry").select2();
    $("#prp_indoor").select2();
    $("#prp_outdoor").select2();
    $("#prp_tenancy").select2();
    $('#prp_zone_id').select2();



    //Extra Images 
    var extraimagesContent = '<div class="row form-row extraimagecontainer">	\n\
		<div class="col-md-3">\n\
		   <input name="prpi_extra_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deleteextraimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addextraimages", function() {
        $(this).parents('.extraimagecontainer').after(extraimagesContent);
        return false;
    });

    $(document).on("click", ".deleteextraimages", function() {
        $(this).parents('.extraimagecontainer').remove();
        return false;
    });



    //360Images 
    var three60imagesContent = '<div class="row form-row 360imagecontainer">	\n\
		<div class="col-md-3">\n\
		   <input name="prpi_360_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger delete360images">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".add360images", function() {
        $(this).parents('.360imagecontainer').after(three60imagesContent);
        return false;
    });

    $(document).on("click", ".delete360images", function() {
        $(this).parents('.360imagecontainer').remove();
        return false;
    });

    //Floor Images
    var floorimagesContent = '<div class="row form-row floorimagecontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_floor_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deletefloorimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addfloorimages", function() {
        $(this).parents('.floorimagecontainer').after(floorimagesContent);
        return false;
    });

    $(document).on("click", ".deletefloorimages", function() {
        $(this).parents('.floorimagecontainer').remove();
        return false;
    });


    //Bedroom images 
    var bedroomimagesContent = '<div class="row form-row bedroomimagecontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_bedroom_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deletebedroomimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addbedroomimages", function() {
        $(this).parents('.bedroomimagecontainer').after(bedroomimagesContent);
        return false;
    });

    $(document).on("click", ".deletebedroomimages", function() {
        $(this).parents('.bedroomimagecontainer').remove();
        return false;
    });

    //Living Room images
    var livingroomimagesContent = '<div class="row form-row livingimagecontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_living_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deletelivingroomimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addlivingimages", function() {
        $(this).parents('.livingimagecontainer').after(livingroomimagesContent);
        return false;
    });

    $(document).on("click", ".deletelivingroomimages", function() {
        $(this).parents('.livingimagecontainer').remove();
        return false;
    });


    //Living Room images
    var bathroomimagesContent = '<div class="row form-row bathroomimagecontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_bathroom_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deletebathroomimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addbathroomimages", function() {
        $(this).parents('.bathroomimagecontainer').after(bathroomimagesContent);
        return false;
    });

    $(document).on("click", ".deletebathroomimages", function() {
        $(this).parents('.bathroomimagecontainer').remove();
        return false;
    });


    //Outdoor images
    var outdoorimagesContent = '<div class="row form-row outdoorimagecontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_outdoor_image[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deleteoutdoorimages">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".addoutdoorimages", function() {
        $(this).parents('.outdoorimagecontainer').after(outdoorimagesContent);
        return false;
    });

    $(document).on("click", ".deleteoutdoorimages", function() {
        $(this).parents('.outdoorimagecontainer').remove();
        return false;
    });



    //Document
    var outdoorimagesContent = '<div class="row form-row documentcontainer">\n\
		<div class="col-md-3">\n\
		   <input name="prpi_document[]" class="image_class" type="file" />\n\
                </div>\n\
                <div class="col-md-2">\n\
                    <button type="button" class="btn btn-danger deletedocument">Delete</button>\n\
                </div>\n\
	   </div>';


    $(document).on("click", ".adddocument", function() {
        $(this).parents('.documentcontainer').after(outdoorimagesContent);
        return false;
    });

    $(document).on("click", ".deletedocument", function() {
        $(this).parents('.documentcontainer').remove();
        return false;
    });




    $('.property-select').change(function() {
        
        
        $('.propertytype').hide();
        
        var postdata = {};
        var property_type = $(this).val();
        if (property_type === '') {
        } else {
            
            //Logic for show and hide of the 
            if (property_type === '1') {
                //If property type is house type   
                $('.housetype').show();
                
            }else if(property_type === '2'){
                //apartment  
                $('.apartmenttype').show();
            }
            else if(property_type === '5'){
                //commercial
                    $('.commercialtype').show();
            }
            else if (property_type === '3'  ) {
                //If property type is plot 
                $('.plottype').show();
            }else if(property_type === '4' ){
                //If property type is  land type
                $('.landtype').show();
            }
            else if (property_type === '6'  ) {
                //If property type is BUSINESS SALE
                $('.businesstype').show();
            }else if (property_type === '7'  ) {
                //If property type is BUILDING SALE
                $('.buildingtype').show();
            } else {
                $('.propertytype').hide();
            }
            
            postdata['property_type'] = property_type;
            jQuery.post(BASE_PATH + '/property/getpropertysublist', postdata, function(result) {
                $('.property-subtype-select').html(result);
                if ($('#property_hdn_subtype').val() === '') {

                } else {
                    $('#property_subtype').val($('#property_hdn_subtype').val());
                }
            });
        }

    });


    $('.property-select').trigger('change');

    $('.member-country').change(function() {
        var postdata = {};
        var country_id = $(this).val();
        if (country_id === '') {
        } else {
            postdata['country_id'] = country_id;
            jQuery.post(BASE_PATH + '/property/getcitylist', postdata, function(result) {
                $('.member-city').html(result);
            });
        }

    });


    $('.property-country').change(function() {
        var postdata = {};
        var country_id = $(this).val();
        if (country_id === '') {
        } else {
            postdata['country_id'] = country_id;
            jQuery.post(BASE_PATH + '/property/getcitylist', postdata, function(result) {
                $('.property-city').html(result);
            });
            //$('#prp_area').html('<option>-- Select Area -- </option>');
            $('.property-enoria').html('<option value="">-- Select Enoria -- </option>');
        }

    });



    $('.property-city').change(function() {
        var postdata = {};
        var city_id = $(this).val();
        if (city_id === '') {
        } else {
            postdata['city_id'] = city_id;
            jQuery.post(BASE_PATH + '/property/getarealist', postdata, function(result) {
                $('#prp_area').html(result);
            });

            /* jQuery.post(BASE_PATH + '/property/getenorialist', postdata, function(result) {
                $('.property-enoria').html(result);
            });*/
            
            jQuery.post(BASE_PATH + '/property/zonelist', postdata, function(result) {
                $('.prp-zone').html(result);
               
            });



        }

    });
    
    
    
    $('.property-area').change(function() {
        var postdata = {};
        var area_id = $(this).val();
        if (area_id === '') {
        } else {
            postdata['area_id'] = area_id;
            jQuery.post(BASE_PATH + '/property/getenorialist', postdata, function(result) {
                $('.property-enoria').html(result);
            });

           /* jQuery.post(BASE_PATH + '/property/zonelist', postdata, function(result) {
                $('.prp-zone').html(result);
               
            });
*/

        }

    });






    $(".wysihtml5-editor").each(function() {
        $(this).wysihtml5({
            "font-styles": true, //Font styling, e.g. h1, h2, etc. Default true
            "emphasis": true, //Italics, bold, etc. Default true
            "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
            "html": false, //Button which allows you to edit the generated HTML. Default false
            "link": false, //Button to insert a link. Default true
            "image": false, //Button to insert an image. Default true,
            "color": false//Button to change color of font  
        });
    });


    var geocoder;
    var map;
    var defaultLat = '';
    var defaultLon = '';
    if (LATITUDE === '') {
        defaultLat = 37.98;
    } else {
        defaultLat = LATITUDE;
    }
    if (LONGITUDE === '') {
        defaultLon = 23.73;
    } else {
        defaultLon = LONGITUDE;
    }
    geocoder = new google.maps.Geocoder();


    var latlng = new google.maps.LatLng(defaultLat, defaultLon);

    if (ISKMLPRESENT === 'YES') {
        var mapOptions = {
            zoom: 25,
            center: latlng
        }
    } else {
        var mapOptions = {
            zoom: 10,
            center: latlng
        }
    }

    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    if (ISKMLPRESENT === 'YES') {
        var bermudaTriangle;
        var triangleCoords = new Array();

        var markerArray = JSON.parse(MARKERS);

        $.each(markerArray, function(index, value) {
            var latitudelongitude = new google.maps.LatLng(value.lat, value.lon);
            triangleCoords.push(latitudelongitude);
        });


        // Construct the polygon.
        bermudaTriangle = new google.maps.Polygon({
            paths: triangleCoords,
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35
        });

        if (!google.maps.Polygon.prototype.getBounds) {

            google.maps.Polygon.prototype.getBounds = function() {
                var bounds = new google.maps.LatLngBounds()
                this.getPath().forEach(function(element, index) {
                    bounds.extend(element)
                })
                return bounds
            }

        }

        var polygonlatlng = new google.maps.LatLng(bermudaTriangle.getBounds().getCenter().lat(), bermudaTriangle.getBounds().getCenter().lng());
        map.setCenter(polygonlatlng);
        bermudaTriangle.setMap(map);

        marker = new google.maps.Marker({
            map: map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: polygonlatlng
        });



    } else {

        marker = new google.maps.Marker({
            map: map,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: latlng
        });


    }

    google.maps.event.addListener(marker, 'dragend', function(event) {

        latitude = event.latLng.lat();
        longitude = event.latLng.lng();

        jQuery('#prp_lat').val(latitude);
        jQuery('#prp_lon').val(longitude);

        var draglatlng = new google.maps.LatLng(event.latLng.lat(), event.latLng.lng());
        geocoder.geocode({'latLng': draglatlng}, function(data, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var add = data[1].formatted_address; //this is the full address
                jQuery('#prp_address').val(add);
            } else {
                alert('Geocode was not successful for finding address for following reason: ' + status);
            }
        });
    });
	
	$('#current-location').click(function() {
		
		// Try HTML5 geolocation
		if(navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(position) {
			var pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);
			
			geocoder.geocode({'latLng': pos}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					if (results[1]) {
						jQuery('#prp_address').val(results[1].formatted_address);
					}
				} else {
					//alert("Geocoder failed due to: " + status);
				}
			});
			
			
			jQuery('#prp_lat').val(position.coords.latitude);
			jQuery('#prp_lon').val(position.coords.longitude);
			
			map.setCenter(pos);
			marker.setPosition(pos);
			
			//map.setCenter(pos);
			});
		} else {
			alert(" Browser doesn't support Geolocation");
			// Browser doesn't support Geolocation
			//handleNoGeolocation(false);
		}
		
	});
	
	
    $('#search-map').click(function() {

        var address = document.getElementById('prp_address').value;
        geocoder.geocode({'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();

                jQuery('#prp_lat').val(latitude);
                jQuery('#prp_lon').val(longitude);

                map.setCenter(results[0].geometry.location);
				marker.setPosition(results[0].geometry.location);
				/*
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
				*/
            } else {
                alert('Geocode was not successful for the following reason: ' + status);

            }
        });
    });



    $('#rootwizard').bootstrapWizard({
        'tabClass': 'form-wizard',
                
        'onTabShow' : function(tab, navigation, index) {
			
			
			
			//Scroll Logic
			$("html, body").animate({
					scrollTop:0
				},"slow");
			
			
            $('#map-canvas').css({height:400,width:800});
            
            x = map.getZoom();
                    c = map.getCenter();
                    google.maps.event.trigger(map, 'resize');
                    map.setZoom(x);
                    map.setCenter(c);
					
			$('.submitfrmbtn').removeClass('display-none');
            $('.lastbutton').removeClass('display-none');
					
			if (index === 3) {
				$('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
			} else {
				$('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
			}
					
        },         
        'onTabClick': function(tab, navigation, index) {
        },
        'onNext': function(tab, navigation, index) {
                
            $('.submitfrmbtn').removeClass('display-none');
            $('.lastbutton').removeClass('display-none');

            if (index === 3) {
                $('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
            } else {
                $('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
            }
        },
        'onPrevious': function(tab, navigation, index) {
			
            if (index === 3) {
                $('.submitfrmbtn').removeClass('display-none');
                $('.lastbutton').addClass('display-none');
            } else {
                $('.submitfrmbtn').addClass('display-none');
                $('.lastbutton').removeClass('display-none');
            }
        }
    });

});
	 